// import request from '@/utils/request'

export function fetchImages(params) {
  return request({
    url: '/api/images',
    method: 'get',
    params
  })
}

export function addImage(data) {
  return request({
    url: '/api/images',
    method: 'post',
    data
  })
}

export function updateImage(id, data) {
  return request({
    url: `/api/images/${id}`,
    method: 'put',
    data
  })
}

export function deleteImage(id) {
  return request({
    url: `/api/images/${id}`,
    method: 'delete'
  })
}

export function downloadImage(id) {
  return request({
    url: `/api/images/${id}/download`,
    method: 'get',
    responseType: 'blob'
  }).then(response => {
    // 处理下载逻辑
    const url = window.URL.createObjectURL(new Blob([response.data]))
    const link = document.createElement('a')
    link.href = url
    link.setAttribute('download', `image_${id}.tif`)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  })
}

export function uploadImage(file) {
  const formData = new FormData()
  formData.append('file', file)
  return request({
    url: '/api/upload',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}